package vn.evolus.pencil.initialization;

import java.util.Calendar;
import java.util.HashSet;

import vn.evolus.commons.data.IRepository;
import vn.evolus.pencil.model.Collection;
import vn.evolus.pencil.model.Role;
import vn.evolus.pencil.model.User;
import vn.evolus.pencil.service.RoleService;
import vn.evolus.pencil.service.UserService;

public class InitDataService {
	private UserService userService;
	private RoleService roleService;
	
	public InitDataService(UserService userService, RoleService roleService) {
		this.userService = userService;
		this.roleService = roleService;
		
		if (roleService.getAllRoles().size() == 0){
			createRoleUser();
			createUserForTest();
			//createDBCollections();
		}
		
	}
	
	private void createDBCollections(){
		//Set<Collection> listItems = new HashSet<Collection>();
		
		User firstUser = userService.getUserById(1l);

		Collection itemCollection1 = new Collection();
		itemCollection1.setName("name1");
		itemCollection1.setCollectionId("collectionId1");
		itemCollection1.setDescription("description1");
		itemCollection1.setAuthor("author1");
		itemCollection1.setLastUpdate(Calendar.getInstance().getTime());
		itemCollection1.setIcon("http://icon");
		itemCollection1.setPackageUrl("http://package");
		itemCollection1.setUser(firstUser);
		
		Collection itemCollection2 = new Collection();
		itemCollection2.setName("name2");
		itemCollection2.setDescription("description2");
		itemCollection2.setCollectionId("collectionId2");
		itemCollection2.setAuthor("author2");
		itemCollection2.setLastUpdate(Calendar.getInstance().getTime());
		itemCollection2.setIcon("http://icon");
		itemCollection2.setPackageUrl("http://package");
		itemCollection2.setUser(firstUser);

		firstUser.setCollections(new HashSet<Collection>());
		firstUser.getCollections().add(itemCollection1);
		firstUser.getCollections().add(itemCollection2);
		
		userService.updateUser(firstUser);
	}
	
	private void createRoleUser(){
		if (roleService.getUserRole() == null) {
        	roleService.createUserRole();
        }
	}
	
	private void createUserForTest(){
		User user = new User();
    	user.setEmail("admin@rumble.com");
    	user.setUsername("bu");
    	user.setPassword("123456");
    	user.setEnabled(true);
    	
    	userService.saveUser(user);
	}
	
}
